//package com.bussiness.controller;
//
//import com.alibaba.fastjson.JSON;
//import com.alibaba.fastjson.JSONArray;
//import com.alibaba.fastjson.JSONObject;
//import com.bussiness.model.Article;
//import com.bussiness.model.IkHotWords;
//import com.bussiness.service.EsTestService;
//import com.common.response.ResponseBean;
//import com.common.response.impl.BaseResponseImpl;
//import com.elastic.model.CreateIndexBean;
//import com.elastic.util.Esutil;
//import io.swagger.annotations.Api;
//import io.swagger.annotations.ApiOperation;
//import io.swagger.annotations.ApiParam;
//import org.elasticsearch.common.unit.TimeValue;
//import org.elasticsearch.index.query.*;
//import org.elasticsearch.search.builder.SearchSourceBuilder;
//import org.slf4j.Logger;
//import org.slf4j.LoggerFactory;
//import org.springframework.web.bind.annotation.*;
//
//import javax.annotation.Resource;
//import java.util.List;
//import java.util.UUID;
//import java.util.concurrent.TimeUnit;
//
///**
// * @description: es测试
// * @author: zyj
// * @create: 2019-12-05 11:28
// **/
//@RestController
//@Api(tags={"es测试"})
//@RequestMapping("/es")
//public class EsTestController extends BaseResponseImpl {
//    Logger logger = LoggerFactory.getLogger(this.getClass());
//    @Resource
//    private Esutil esutil;
//
//    @Resource
//    private EsTestService esTestService;
//
//
//    @ApiOperation(value="添加索引")
//    @RequestMapping(value = "/createIndex", method = RequestMethod.PUT)
//    public ResponseBean createIndex(@RequestBody List<CreateIndexBean> list,@ApiParam(value = "索引名称") @RequestParam String indexName) {
//        return successData(esutil.createIndex(list, indexName));
//    }
//
//
//    @ApiOperation(value="添加数据")
//    @RequestMapping(value = "/insertData", method = RequestMethod.PUT)
//    public ResponseBean insertData(@RequestBody Article article,@RequestParam String indexName) {
//        return successMsg(String.valueOf(esutil.inseartData(JSONObject.toJSONString(article),indexName)));
//    }
//
//    @ApiOperation(value="批量添加数据")
//    @RequestMapping(value = "/insertDataBatch", method = RequestMethod.PUT)
//    public ResponseBean insertDataBatch(@RequestBody List<Article> article,@RequestParam String indexName) {
//        return successMsg(String.valueOf(esutil.inseartDataBatch(JSONArray.parseArray(JSON.toJSONString(article)), indexName)));
//    }
//
//    @ApiOperation(value="批量添加数据")
//    @RequestMapping(value = "/insertDataBatchLocal", method = RequestMethod.PUT)
//    public ResponseBean insertDataBatchLocal(@RequestParam String indexName) {
//        Long startTime = System.currentTimeMillis();
//
//        //模拟1000w数据
//        for(int i=0;i<100;i++){
//            JSONArray jsonArray = new JSONArray();
//            for(int j=0;j<100;j++){
//                int id = (int)(Math.random()*1000000);
//                JSONObject jsonObject = new JSONObject();
//                jsonObject.put("id", id);
//                jsonObject.put("time", esTestService.getRandomTime());
//                jsonObject.put("context", esTestService.getContext());
//                jsonObject.put("title", esTestService.getArticle());
//                jsonObject.put("writer", esTestService.getAuth());
//                jsonObject.put("rowKey", UUID.randomUUID().toString().replace("-",""));
//                jsonArray.add(jsonObject);
//            }
//            esutil.inseartDataBatch(jsonArray, indexName);
//        }
//        Long endTime = System.currentTimeMillis();
//        System.out.println(endTime-startTime);
//        return success();
//    }
//
//    @ApiOperation(value="查询数据")
//    @RequestMapping(value = "/queryData", method = RequestMethod.GET)
//    public ResponseBean queryData(@RequestParam String indexName,@RequestParam String key,@RequestParam String value
//            ,@RequestParam int page,@RequestParam int size){
//        try {
//            //如果用name直接查询，其实是匹配name分词过后的索引查到的记录(倒排索引)；如果用name.keyword查询则是不分词的查询，正常查询到的记录
//            /**范围查询*/
//           // RangeQueryBuilder rangeQueryBuilder = QueryBuilders.rangeQuery("time").from("1991-01-01").to("2010-10-10").format("yyyy-MM-dd");
//             /**精准查询*/
//            //TermQueryBuilder termQueryBuilder = QueryBuilders.termQuery("name.keyword", name);
//             /**前缀查询*/
//            //PrefixQueryBuilder prefixQueryBuilder = QueryBuilders.prefixQuery("title.keyword", "String");
//             /**通配符查询*/
//            //WildcardQueryBuilder wildcardQueryBuilder = QueryBuilders.wildcardQuery("name.keyword", "*三");
//             /**模糊查询*/
//            //FuzzyQueryBuilder fuzzyQueryBuilder = QueryBuilders.fuzzyQuery("name", "三");
//            FuzzyQueryBuilder fuzzyQueryBuilder = QueryBuilders.fuzzyQuery(key, value);
//
//             /**查询条件拼接  should:查询条件or的关系*/
//            BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
//            boolQueryBuilder.filter(fuzzyQueryBuilder);
//            //boolQueryBuilder.filter(rangeQueryBuilder).should(prefixQueryBuilder);
//            /**排序*/
//            SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
////            SortBuilder fieldSortBuilder = SortBuilders.fieldSort("title");
////            fieldSortBuilder.order(SortOrder.DESC);
//
//             /**查询条件*/
//            sourceBuilder.query(boolQueryBuilder);
//            /**排序*/
//            //sourceBuilder.sort(new FieldSortBuilder("id").order(SortOrder.DESC));
//             /** 第几页*/
//             int start = (page-1)*size;
//            sourceBuilder.from(start);
//             /** 每页多少条数据*/
//            sourceBuilder.size(size);
//             /** 获取的字段（列）和不需要获取的列*/
//            //sourceBuilder.fetchSource(new String[]{"time", "title"}, new String[]{"writer"});
//             /**超时时间*/
//            sourceBuilder.timeout(new TimeValue(60, TimeUnit.SECONDS));
//            Long startTime = System.currentTimeMillis();
//            JSONArray jsonArray = esutil.queryIndex(sourceBuilder, indexName);
//            Long endTime = System.currentTimeMillis();
//            System.out.println("查询时间："+(endTime-startTime));
//            return successData(jsonArray);
//        } catch (Exception e){
//            e.printStackTrace();
//        }
//
//        return error();
//
//    }
//
//
//    @ApiOperation(value="删除索引")
//    @RequestMapping(value = "/deleteIndex", method = RequestMethod.DELETE)
//    public ResponseBean deleteIndex(@RequestParam String indexName){
//        return successMsg(String.valueOf(esutil.deleteIndex(indexName)));
//    }
//
//    @ApiOperation(value="删除数据")
//    @RequestMapping(value = "/deleteData", method = RequestMethod.DELETE)
//    public ResponseBean deleteData(@RequestParam String id, @RequestParam String indexName){
//        return successMsg(String.valueOf(esutil.deleteData(id, indexName)));
//    }
//
//    @ApiOperation(value="批量删除数据")
//    @RequestMapping(value = "/deleteDataBatch", method = RequestMethod.DELETE)
//    public ResponseBean deleteDataBatch(@RequestBody List<String> ids, @RequestParam String indexName){
//        return successMsg(String.valueOf(esutil.deleteDataBatch(ids, indexName)));
//    }
//
//    @ApiOperation(value="添加自定义词典")
//    @RequestMapping(value = "/createDictionary", method = RequestMethod.PUT)
//    public ResponseBean createDictionary(@RequestParam String word){
//        try {
//            esTestService.saveIkHot(word);
//        } catch (Exception e) {
//            e.printStackTrace();
//            return error();
//        }
//        return success();
//    }
//
//    @ApiOperation(value="修改自定义词典")
//    @RequestMapping(value = "/updateDictionary", method = RequestMethod.PUT)
//    public ResponseBean updateDictionary(@RequestBody IkHotWords ikHotWords){
//        try {
//            esTestService.update(ikHotWords);
//        } catch (Exception e) {
//            e.printStackTrace();
//            return error();
//        }
//        return success();
//    }
//
//    @ApiOperation(value="获取自定义词典列表")
//    @RequestMapping(value = "/ikHotWordsList", method = RequestMethod.GET)
//    public ResponseBean ikHotWordsList(){
//        try {
//            return successData(esTestService.ikHotWordsList());
//        } catch (Exception e) {
//            e.printStackTrace();
//            return error();
//        }
//    }
//
//    @ApiOperation(value="自定义词典分词")
//    @RequestMapping(value = "/participle", method = RequestMethod.PUT)
//    public ResponseBean participle(@RequestParam String data,@RequestParam String type){
//        try {
//            return successData(esutil.getIkAnalyzeSearchTerms(data, type));
//        } catch (Exception e) {
//            e.printStackTrace();
//            return error();
//        }
//    }
//
//}
